/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.oop.example.work.bel;

import com.oop.example.processing.Parser;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author -
 */
public class ParserFactory {

    private static List<Class<? extends Parser>> persers = new ArrayList<Class<? extends Parser>>();

    static {
        persers.add(CommonParser.class);
    }

    public static Parser getParser(EntryHandler handler) throws NoSuchParserException {
        for (Class parserClazz : persers) {
            try {
                Parser parser = (Parser) parserClazz.newInstance();
                //parser.setHandler(handler);
                if (parser.isApplicable()) {
                    //System.out.println("return " + parser.getClass().getName());
                    return parser;
                }
            } catch (Exception ex) {
                if (ex instanceof RuntimeException) {
                    throw (RuntimeException)ex;
                }
                //ex.printStackTrace(System.out);
            }
        }
        throw new NoSuchParserException();
    }
}
